import { Form, FormProps, Button, Space } from 'antd';
import { useState } from 'react';

interface ProFormProps extends FormProps {
  onSubmit?: (values: any) => Promise<void>;
  onCancel?: () => void;
  submitText?: string;
  cancelText?: string;
}

export function ProForm({
  children,
  onSubmit,
  onCancel,
  submitText = '确定',
  cancelText = '取消',
  ...props
}: ProFormProps) {
  const [loading, setLoading] = useState(false);
  const [form] = Form.useForm();

  const handleSubmit = async (values: any) => {
    if (!onSubmit) return;
    setLoading(true);
    try {
      await onSubmit(values);
      form.resetFields();
    } finally {
      setLoading(false);
    }
  };

  return (
    <Form
      form={form}
      onFinish={handleSubmit}
      layout="vertical"
      {...props}
    >
      {children}
      <Form.Item className="mb-0 text-right">
        <Space>
          {onCancel && (
            <Button onClick={onCancel}>{cancelText}</Button>
          )}
          <Button type="primary" htmlType="submit" loading={loading}>
            {submitText}
          </Button>
        </Space>
      </Form.Item>
    </Form>
  );
}